/**
   Counts the number of times a character appears in a string.
*/
public class CharCounter
{
   /**
      Returns the number of times that a target character
      appears in a string.
      @param source the input string
      @param ch the character to be counted (a string of length 1)
      @return the number of times the target character
      appears in the source
   */
   public static void main(String[] args)
   {
      String str = "abracadabra";
      String ch = "a";
      int sum = countChar(str, ch);
      System.out.println("Character appears " + sum +  " times.");
   }
   
   public static int countChar(String source, String ch)
   {
      int sum = 0;
      int len = source.length();
      for(int i = 0; i < len; i++)
      {
         String a = source.charAt(i) + "";
         if(a.equals(ch))
         {
            sum++;
         }
      }
      return sum;
   }
}
